# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/3/31
desc: 
sample: 
"""
# f = open('test.txt','r')
# f.write('test')
# f.close()

# 单纯写 覆盖
# with open('test.txt','w',encoding='utf-8') as f:
#     f.write("test")

# 追加
# with open('test.txt','a',encoding='utf-8') as f:
#     f.write("hello3" + "\n")

# 读全部
# with open("test.txt", "r") as source_file:
#     content_to_append = source_file.read()
#     print(content_to_append)

# 读一行
# with open("test.txt", "r") as source_file:
#     content_to_append = source_file.readlines()
#     print(content_to_append)
import csv
import os

# 路径问题
# print(os.path.dirname(__file__)) # 返回目录路径
# base_path = os.path.dirname(__file__)
# # 把生成的报告放到reports目录下
# report =os.path.join(base_path, 'reports', 'report.txt')
# print(report) # 将目录和文件名合成一个路径
# # os.makedirs(report)
# print(os.path.abspath(__file__)) # 输出绝对路径
#
# jsonfile = os.path.join(base_path, 'data', 'json.txt')
# with open(jsonfile, "r",encoding="utf-8") as source_file:
#     content_to_append = source_file.read()
#     print(content_to_append)
#
# csvfile = os.path.join(base_path, 'data', 'id.csv')
#
#
# with open(csvfile, "r",encoding="utf-8") as source_file:
#     reader = csv.reader(source_file)
#     next(reader)
#     mydata = list(reader)
#
# print(mydata)
#
# with open(csvfile, "r",encoding="utf-8") as source_file:
#     reader = csv.DictReader(source_file)
#     data = list(reader)
#
#
#
# print(data)
from openpyxl import load_workbook


def getcsvlist(file_name, header=True):
    base_path = os.path.dirname(__file__)
    csvfile = os.path.join(base_path, 'data', file_name)
    with open(csvfile, "r", encoding="utf-8") as source_file:
        reader = csv.reader(source_file)
        if header:
            next(reader)
        mydata = list(reader)
    return mydata


def getcsvDict(file_name):
    base_path = os.path.dirname(__file__)
    csvfile = os.path.join(base_path, 'data', file_name)
    with open(csvfile, "r", encoding="utf-8") as source_file:
        reader = csv.DictReader(source_file)
        mydata = list(reader)
    return mydata


from openpyxl import load_workbook


#
# base_path = os.path.dirname(__file__)
# excelfile = os.path.join(base_path, 'data', 'case.xlsx')
# # 加载Excel工作簿
# wb = load_workbook(excelfile)
#
# # 选择要读取的工作表
# ws = wb['Sheet1']
# # 遍历行和列读取数据

# row1 = ws.iter_rows(values_only=True,min_row=1)
# header_info = [cell.value for cell in ws[1]]
# print(header_info)
#
# for row in ws.iter_rows(values_only=True,min_row=2):
#     print(row)


def getExcelDict(file_name, sheet_name='Sheet1'):
    base_path = os.path.dirname(__file__)
    excelfile = os.path.join(base_path, 'data', file_name)
    # 加载 Excel 工作簿
    wb = load_workbook(excelfile)
    # 获取指定工作表
    ws = wb[sheet_name]
    # 初始化空列表用于存放数据
    data_list = []
    header_info = [cell.value for cell in ws[1]]
    # 遍历工作表中的所有行
    for row in ws.iter_rows(values_only=True, min_row=2):
        # 数据转变成字典
        row_dict = dict(zip(header_info, row))
        data_list.append(row_dict)
    return data_list


def getExcelList(file_name, sheet_name='Sheet1'):
    base_path = os.path.dirname(__file__)
    excelfile = os.path.join(base_path, 'data', file_name)
    # 加载 Excel 工作簿
    wb = load_workbook(excelfile)
    # 获取指定工作表
    ws = wb[sheet_name]
    # 初始化空列表用于存放数据
    data_list = []
    # 遍历工作表中的所有行
    for row in ws.iter_rows(values_only=True, min_row=2):
        # 将当前行的单元格值存入列表
        data_list.append(row)
    return data_list


#
# print(getcsvlist('id.csv'))
# print(getcsvDict('id.csv'))

# print(getExcelList('case.xlsx'))

print(getExcelDict('case.xlsx'))
